package com.thunbu.thunbu_meeting_system


import android.util.Log
import com.hjq.toast.ToastUtils
import com.honeywell.rfidservice.ConnectionState
import com.honeywell.rfidservice.RfidManager
import com.honeywell.rfidservice.TriggerMode
import com.honeywell.rfidservice.rfid.RfidReader
import com.thunbu.lib_common.BuildConfig
import com.thunbu.lib_common.base.BaseApplication
import com.thunbu.lib_common.exception.CrashHelper
import com.thunbu.lib_common.utils.ActivityManager
import com.thunbu.lib_common.utils.MFSPHelper
import com.thunbu.lib_common.utils.okhttp.OkHttpUtils
import me.jessyan.autosize.AutoSizeConfig


/**
 *Created by Chaolue.Fan on 2021/5/13
描述:
 */
class App : BaseApplication() {

    companion object {
        lateinit var instance: App
    }


    init {
        Log.i("App", "init")
    }


    var rfidMgr: RfidManager? = null
    var mRfidReader: RfidReader? = null

    override fun onCreate() {
        Log.i("App", "onCreate")
        super.onCreate()
        instance = this

        init()
        initCrash()


    }

    private fun init() {
        AutoSizeConfig.getInstance().isBaseOnWidth = true
        ToastUtils.init(this)
        MFSPHelper.initialize(this)
        //Bugly.init(this, BuildConfig.BUGLY_APP_ID, BuildConfig.DEBUG)
        registerActivityLifecycleCallbacks(ActivityManager.instance)
        OkHttpUtils.getInstance().setCertificates()


    }

    private fun initCrash() {
        CrashHelper.turnOnReadErrorInfoPermissions { t, u ->
            if (BuildConfig.DEBUG) {
                t?.run()
            }
        }
    }


    fun checkIsRFIDReady(): Boolean {
        if (rfidMgr?.getConnectionState() != ConnectionState.STATE_CONNECTED)
            return false
        if (rfidMgr?.readerAvailable()?.not() == true) {
            return false
        }
        if (rfidMgr?.getTriggerMode() != TriggerMode.RFID) {
            return false
        }
        return true
    }

     fun isReaderAvailable(): Boolean {
        return mRfidReader?.available()?:false
    }

     fun disconnect() {
        rfidMgr?.disconnect()
        rfidMgr?.setDevicePower(false)
    }

}